<?php
namespace app\login\controller;
use app\admin\model\User;
use app\admin\service\Token;
use think\Controller;
use think\Request;

class Login extends Controller{
    public function login () {
        $token = Token::getToken();
        $flag = "false";
        if(array_key_exists("username",$token)){
            $flag = "true";
        }
        $token["flag"] = $flag;
        $this -> assign("token",$token);
        return $this->fetch();
    }
    public function getLogin(){
        $request = Request::instance();
        $data = $request -> post();
        if($data["username"] == ""){
            $this -> error("请输入账号");
        }
        if($data["password"] == ""){
            $this -> error("请输入密码");
        }
        $user = User::getLogin($data["username"]);
        if($user == null){
            $this -> error("该账号不存在");
        }
        if(array_key_exists('password', $user)){
            $this -> error("该账号不存在");
        }
        if ($user["password"] != md5($data["password"])) {
            $this -> error("密码输入有误");
        }
        if($user["status"] <= 0){
            $this -> error("该账号被禁用了");
        }
        User::getLastTime($user->id);
        Token::setToken($user);
        $this -> success("成功登录","Admin/Index/index");
     }
}
